<?php
class post
{
    public static function autoLogin():bool{
        function go(){
            $loginLink=linkProcess("{LINK_login}");
            session_delete("isLogin");
            session_delete("autoLogin");
            session_delete("username");
            session_delete("password");
            echo json_encode(["status"=>false,"msg"=>"登录信息失效，请刷新页面。"]);
            die();
        }
        $isLogin=session_get("isLogin");
        if($isLogin=="yes"){
            writeLog("Info","尝试自动登录");
            $username = session_get("username");
            writeLog("Info","用户名为：".$username);
            $password = session_get("password");
            $conn = connectDB();
            if(!$conn){
                writeLog("Error","数据库连接失败，回到登录页面。");
                go();
            }
            $sql="select * from `userList` where `username`='$username';";
            writeLog("Info","数据库尝试执行");
            $result=mysqli_query($conn,$sql);
            if(!$result){
                writeLog("Error","数据库执行失败，回到登录页面。");
                go();
            }
            if(mysqli_num_rows($result)>0){
                $row=mysqli_fetch_assoc($result);
                if($row["ban"]=="yes"){
                    writeLog("Error","用户阻止登录，返回登录页面。");
                    go();
                }else{
                    writeLog("Info","自动登录验证成功。");
                }
            }else{
                writeLog("Error","用户不存在，返回登录页面。");
                go();
            }
        }else{
            writeLog("Error","用户未登录，返回登录页面。");
            go();
        }
        return false;
    }

    public static function login_callback_iurt():bool
    {
        writeLog("Info","尝试从IURT登录");
        echo "登录成功，欢迎用户：<mark>".args_post("username")."</mark>。<br><img src='".args_post("head_picture")."' width='300'>";
        $sql="select * from `userBind` where `iurt`='".args_post("username")."';";
        $conn=connectDB();
        if(!$conn){
            loadErrorPage("错误","加载绑定信息失败，数据库连接失败。");
        }
        $result3=mysqli_query($conn,$sql);
        if(!$result3){
            loadErrorPage("错误","加载绑定信息失败，数据库执行失败。");
        }
        if(mysqli_num_rows($result3)==0){
            loadErrorPage("错误","该用户未绑定，而绑定功能还没完成(doge");
        }else{
            $row=mysqli_fetch_assoc($result3);
            loadErrorPage("提示","登录成功，欢迎：".$row["username"],false);
            session_set("username",$row["username"]);
            session_set("isLogin","yes");
            echo "<script>setTimeout(()=>{location='".linkProcess("{LINK_user}")."'},1000);</script>";
        }
        return true;
    }

    public static function login_callback_qq():bool
    {
        echo "登录成功，欢迎用户：<mark>".args_post("name")."</mark>。<br><img src='".args_post("avatar")."' width='300'>";
        $id=args_post("openid");
        echo "<br>openid=<mark>$id</mark>";
        $sql="select * from `userBind` where `qq`='".args_post("openid")."';";
        writeLog("Info","SQL = $sql");
        $conn=connectDB();
        if(!$conn){
            loadErrorPage("错误","加载绑定信息失败，数据库连接失败：".mysqli_connect_error());
        }
        $result3=mysqli_query($conn,$sql);
        if(!$result3){
            loadErrorPage("错误","加载绑定信息失败，数据库执行失败：".mysqli_error($conn));
        }
        if(mysqli_num_rows($result3)==0){
            loadErrorPage("错误","该用户未绑定，而绑定功能还没完成(doge");
        }else{
            $row=mysqli_fetch_assoc($result3);
            $sql_2="update `userBind` set `qq_name`='".args_post("name")."' where `qq`='".args_post("openid")."';";
            $result_2=mysqli_query($conn,$sql_2);
            if(!$result_2){
                loadErrorPage("错误","数据更新失败：".mysqli_error($conn));
            }
            loadErrorPage("提示","登录成功，欢迎：".$row["username"],false);
            session_set("username",$row["username"]);
            session_set("isLogin","yes");
            echo "<script>setTimeout(()=>{location='".linkProcess("{LINK_user}")."'},1000);</script>";
        }
        // print_r($_POST);
        return true;
    }

    public static function register_sendMail():bool
    {
        writeLog("Info","发送邮件验证码");
        $email = args_post("email");
        $code = rand(1111,9999);
        writeLog("Info","验证码为 $code");
        $result = sendMail("感谢您注册星辰工作室","感谢您注册星辰工作室用户，本次验证码为：$code ，请及时使用，重新发送邮件后此验证码将失效。",$email,"星辰工作室");
        $data = ["status"=>$result];
        if($result){
            $data["code"]=$code;
            session_set("register_email",$email);
            writeLog("Info","验证码发送成功");
        }else{
            writeLog("Error","验证码发送失败");
        }
        echo json_encode($data);
        return true;
    }

    public static function register():bool
    {
        writeLog("Info","用户尝试注册");
        $username = args_post("username");
        writeLog("Info","用户名：$username");
        $password = args_post("password");
        $password = string_encode($password,$password,true);
        $email = args_post("email");
        writeLog("Info","邮箱为：$email");
        if(session_get("register_email")!=$email){
            die(json_encode(["status"=>false,"msg"=>"检测到您修改了邮箱，请保证验证前后邮箱地址不变。"]));
        }
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql_1 = "select * from `userList` where username='$username'";
        writeLog("Info","尝试数据库执行第一步");
        $result_1 = mysqli_query($conn,$sql_1);
        if(!$result_1){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据获取成功");
        if(mysqli_num_rows($result_1)>0){
            writeLog("Error","用户已存在");
            die(json_encode(["status"=>false,"msg"=>"用户已存在"]));
        }
        $sql_2 = "select * from `userList` where `email`='$email';";
        writeLog("Info","尝试数据库执行第二步");
        $result_2 = mysqli_query($conn,$sql_2);
        if(!$result_2){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据获取成功");
        if(mysqli_num_rows($result_2)>0){
            writeLog("Error","邮箱已被绑定");
            die(json_encode(["status"=>false,"msg"=>"邮箱已被绑定"]));
        }
        $sql_3 = "insert into `userList` (`username`,`password`,`email`,`time`,`setup`,`ban`,`reason`) values ('$username','$password','$email',now(),'no','no','');";
        writeLog("Info","尝试数据库执行第三步");
        $result_3 = mysqli_query($conn,$sql_3);
        if(!$result_3){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        $sql_4 = "insert into `userBind` (`username`,`iurt`,`github`,`qq`,`qq_name`) values ('$username','no','no','no','no');";
        writeLog("Info","尝试数据库执行第四步");
        $result_4 = mysqli_query($conn,$sql_4);
        if(!$result_4){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","注册成功");
        echo json_encode(["status"=>true,"msg"=>"注册成功！"]);
        return true;
    }

    public static function login():bool
    {
        writeLog("Info","用户尝试登录");
        $username = args_post("username");
        writeLog("Info","用户名：$username");
        $password = args_post("password");
        $autoLogin = args_post("autoLogin");
        writeLog("Info","自动登录 = ".$autoLogin);
        writeLog("Info","清除原有登录信息");
        session_delete("isLogin");
        session_delete("autoLogin");
        session_delete("username");
        session_delete("password");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql_1 = "select * from `userList` where `username`='$username' or `email`='$username'";
        writeLog("Info","尝试数据库执行");
        $result_1 = mysqli_query($conn,$sql_1);
        if(!$result_1){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据获取成功");
        if(mysqli_num_rows($result_1)==0){
            writeLog("Error","用户不存在");
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        $row = mysqli_fetch_assoc($result_1);
        if(string_decode($row["password"],$password,true)==$password){
            if($row["ban"]=="yes"){
                writeLog("Error","用户被阻止登录：".$row["reason"]);
                die(json_encode(["status"=>false,"msg"=>"用户被阻止登录：".$row["reason"]]));
            }else{
                writeLog("Info","获取用户信息");
                $sql_2 = "select * from `userInfo` where `username`='".$row["username"]."';";
                writeLog("Info","尝试数据库执行第二步");
                $result_2 = mysqli_query($conn,$sql_2);
                if(mysqli_num_rows($result_2)>0){
                    $row_2 = mysqli_fetch_assoc($result_2);
                    $avatar = $row_2["avatar"];
                    $nickname = $row_2["nickname"];
                    writeLog("Info","数据获取成功");
                }else{
                    $avatar = "https://user.xcgzs.cn/src/static/user/img/avatar.jpg";
                    $nickname = "昵称未设置";
                    writeLog("Warn","用户没有初始化，暂时忽略问题。");
                }
                writeLog("Info","登录成功");
                echo json_encode(["status"=>true,"msg"=>"登录成功","setup"=>$row["setup"],"avatar"=>$avatar,"nickname"=>$nickname,"username"=>$row["username"],"email"=>$row["email"]]);
                session_set("isLogin","yes");
                session_set("username",$row["username"]);
                session_set("password",$password);
                session_set("autoLogin",$autoLogin);
            }
        }else{
            writeLog("Error","密码错误：$password");
            die(json_encode(["status"=>false,"msg"=>"密码错误"]));
        }
        return true;
    }

    public static function setup_upload():bool
    {
        $username = session_get("username");
        @mkdir(GLOBAL_DATA_PATH);
        @mkdir(DATA_PATH);
        $userDataPath=DATA_PATH.$username;
        @mkdir($userDataPath);
        $newFileName=$userDataPath."/avatar";
        move_uploaded_file($_FILES["file"]["tmp_name"],$newFileName);
        echo "http://".$_SERVER["HTTP_HOST"].substr($newFileName,1);
        return true;
    }

    public static function setup():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $avatar=args_post("avatar");
        $nickname=args_post("nickname");
        $gender=args_post("gender");
        $age=args_post("age");
        $description=args_post("description");
        $conn=connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql_1="select * from `userInfo` where username='$username'";
        writeLog("Info","尝试数据库执行第一步");
        $result_1=mysqli_query($conn,$sql_1);
        if(!$result_1){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库第一步执行成功");
        if(mysqli_num_rows($result_1)==0){
            $sql_2="insert into `userInfo` (`username`,`nickname`,`avatar`,`gender`,`age`,`description`) values ('$username','$nickname','$avatar','$gender','$age','$description');";
        }else{
            $sql_2="update `userInfo` set `nickname`='$nickname',`avatar`='$avatar',`gender`='$gender',`age`='$age',`description`='$description' where `username`='$username';";
        }
        writeLog("Info","尝试数据库执行第二步");
        $result_2=mysqli_query($conn,$sql_2);
        if(!$result_2){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库第二步执行成功");
        writeLog("Info","尝试数据库执行第三步");
        $sql_3="update `userList` set `setup`='yes' where `username`='$username';";
        $result_3=mysqli_query($conn,$sql_3);
        if(!$result_3){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库第三步执行成功");
        writeLog("Info","用户初始化成功");
        echo json_encode(["status"=>true,"msg"=>"用户初始化成功"]);
        return true;
    }

    public static function user_getInfo_basic():bool
    {
        self::autoLogin();
        writeLog("Info","获取用户基本信息");
        $username = session_get("username");
        writeLog("Info","当前用户 $username");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userList` where `username`='$username';";
        writeLog("Info","尝试数据库执行");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            writeLog("Info","用户基本信息获取成功");
            die(json_encode(["status"=>true,"id"=>$row["id"],"username"=>$row["username"],"email"=>$row["email"],"time"=>$row["time"]]));
        }else{
            writeLog("Error","用户不存在");
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_getInfo_info():bool
    {
        self::autoLogin();
        writeLog("Info","获取用户详细信息");
        $username = session_get("username");
        writeLog("Info","当前用户 $username");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            $row=mysqli_fetch_assoc($result);
            writeLog("Info","用户详细信息获取成功");
            die(json_encode(["status"=>true,"id"=>$row["id"],"username"=>$row["username"],"nickname"=>$row["nickname"],"avatar"=>$row["avatar"],"gender"=>$row["gender"],"age"=>$row["age"],"description"=>$row["description"]]));
        }else{
            writeLog("Error","用户未初始化");
            die(json_encode(["status"=>false,"msg"=>"用户未初始化"]));
        }
        return true;
    }

    public static function user_getInfo_bind():bool
    {
        self::autoLogin();
        writeLog("Info","获取用户绑定信息");
        $username = session_get("username");
        writeLog("Info","当前用户 $username");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userBind` where `username`='$username'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            $row=mysqli_fetch_assoc($result);
            $sql_2="select id from `userList` where `username`='$username';";
            $result_2=mysqli_query($conn,$sql_2);
            $row_2=mysqli_fetch_assoc($result_2);
            writeLog("Info","用户详细信息获取成功");
            die(json_encode(["status"=>true,"id"=>$row_2["id"],"username"=>$row["username"],"iurt"=>$row["iurt"],"github"=>$row["github"],"qq"=>$row["qq"],"qq_name"=>$row["qq_name"]]));
        }else{
            writeLog("Error","用户绑定信息不存在");
            die(json_encode(["status"=>false,"msg"=>"用户绑定信息不存在"]));
        }
        return true;
    }

    public static function user_getInfo_permission():bool
    {
        self::autoLogin();
        $list=json_decode(file_get_contents(CONFIG_PATH.getBindName()."/admins.json"),true);
        writeLog("Info","获取用户权限信息");
        $username = session_get("username");
        writeLog("Info","当前用户 $username");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userList` where `username`='$username'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            $row=mysqli_fetch_assoc($result);
            writeLog("Info","用户详细信息获取成功");
            die(json_encode(["status"=>true,"id"=>$row["id"],"username"=>$row["username"],"permission"=>(in_array($username,$list)?"管理员":"普通用户")]));
        }else{
            writeLog("Error","用户绑定信息不存在");
            die(json_encode(["status"=>false,"msg"=>"用户绑定信息不存在"]));
        }
        return true;
    }

    public static function forget_sendMail():bool
    {
        $email = args_post("email");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userList` where `email`='$email'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            writeLog("Info","用户详细信息获取成功");
            $c=rand(1111,9999);
            $a=sendMail("您正在重设您的登录密码","您正在重设您的登录密码，您的验证码是：$c ，请立即使用，重新发送邮件后，这个验证码将失效。",$email,"星辰工作室");
            if($a){
                echo json_encode(["status"=>true,"code"=>$c]);
            }else{
                echo json_encode(["status"=>false]);
            }
        }else{
            writeLog("Error","邮箱不存在");
            die(json_encode(["status"=>false,"msg"=>"邮箱不存在"]));
        }
        return true;
    }

    public static function forget_verify():bool
    {
        $username=args_post("username");
        $email=args_post("email");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="select * from `userList` where `username`='$username' and `email`='$email';";
        $result = mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            session_set("forget_verify","yes");
            echo json_encode(["status"=>true]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户名或邮箱不正确。"]));
        }
        return true;
    }

    public static function forget_setPassword():bool
    {
        $username=args_post("username");
        $password=args_post("password");
        $password=string_encode($password,$password,true);
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        if(session_get("forget_verify")=="yes"){
            $sql="update `userList` set `password`='$password' where `username`='$username';";
            $result=mysqli_query($conn,$sql);
            if(!$result){
                die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
            }
            session_delete("forget_verify");
            echo json_encode(["status"=>true]);
        }else{
            echo json_encode(["status"=>false,"msg"=>"您正在尝试跳过验证步骤，请重新验证。"]);
        }
        return true;   
    }

    public static function user_basic_setpassword_sendMail():bool
    {
        $email = args_post("email");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userList` where `email`='$email'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            writeLog("Info","用户详细信息获取成功");
            $c=rand(1111,9999);
            $a=sendMail("您正在修改您的登录密码","您正在修改您的登录密码，我们需要验证您的身份，您的验证码是：$c ，请立即使用，重新发送邮件后，这个验证码将失效。",$email,"星辰工作室");
            if($a){
                echo json_encode(["status"=>true,"code"=>$c]);
            }else{
                echo json_encode(["status"=>false]);
            }
        }else{
            writeLog("Error","邮箱不存在");
            die(json_encode(["status"=>false,"msg"=>"邮箱不存在"]));
        }
        return true;
    }

    public static function user_basic_setpassword():bool
    {
        $username=session_get("username");
        $password=args_post("password");
        $password=string_encode($password,$password,true);
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        if(session_get("setpassword_verify")=="yes"){
            $sql="update `userList` set `password`='$password' where `username`='$username';";
            $result=mysqli_query($conn,$sql);
            if(!$result){
                die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
            }
            session_delete("setpassword_verify");
            echo json_encode(["status"=>true]);
        }else{
            echo json_encode(["status"=>false,"msg"=>"您正在尝试跳过验证步骤，请重新验证。"]);
        }
        return true;   
    }

    public static function user_basic_setpassword_verify():bool
    {
        $username=session_get("username");
        $email=args_post("email");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="select * from `userList` where `username`='$username' and `email`='$email';";
        $result = mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            session_set("setpassword_verify","yes");
            echo json_encode(["status"=>true]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"邮箱不正确。"]));
        }
        return true;
    }

    public static function user_basic_setemail_sendMail():bool
    {
        self::autoLogin();
        $email = args_post("email");
        $conn = connectDB();
        if(!$conn){
            writeLog("Error","数据库连接失败：".mysqli_connect_error());
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userList` where `email`='$email'";
        writeLog("Info","尝试执行数据库");
        $result = mysqli_query($conn,$sql);
        if(!$result){
            writeLog("Error","数据库执行失败：".mysqli_error($conn));
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        writeLog("Info","数据库执行成功");
        if(mysqli_num_rows($result)>0){
            writeLog("Info","用户详细信息获取成功");
            $c=rand(1111,9999);
            $a=sendMail("您正在修改您的登录密码","您正在修改您的邮箱，我们需要验证您的身份，您的验证码是：$c ，请立即使用，重新发送邮件后，这个验证码将失效。",$email,"星辰工作室");
            if($a){
                echo json_encode(["status"=>true,"code"=>$c]);
            }else{
                echo json_encode(["status"=>false]);
            }
        }else{
            writeLog("Error","邮箱不存在");
            die(json_encode(["status"=>false,"msg"=>"邮箱不存在"]));
        }
        return true;
    }

    public static function user_basic_setemail():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $email=args_post("email");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        if(session_get("setemail_verify")=="yes"){
            $sql_1="select * from `userList` where `email`='$email';";
            $result_1=mysqli_query($conn,$sql_1);
            if(!$result_1){
                die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
            }
            if(mysqli_num_rows($result_1)>0){
                die(json_encode(["status"=>false,"msg"=>"该邮箱已被其他用户绑定"]));
            }
            $sql_2="update `userList` set `email`='$email' where `username`='$username';";
            $result_2=mysqli_query($conn,$sql_2);
            if(!$result_2){
                die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
            }
            session_delete("setemail_verify");
            echo json_encode(["status"=>true]);
        }else{
            echo json_encode(["status"=>false,"msg"=>"您正在尝试跳过验证步骤，请重新验证。"]);
        }
        return true;   
    }

    public static function user_basic_setemail_verify():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $email=args_post("email");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="select * from `userList` where `username`='$username' and `email`='$email';";
        $result = mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            session_set("setemail_verify","yes");
            echo json_encode(["status"=>true]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"邮箱不正确。"]));
        }
        return true;
    }

    public static function user_info_nickname_get():bool
    {
        self::autoLogin();
        $username = session_get("username");
        $conn = connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            echo json_encode(["status"=>true,"data"=>$row["nickname"]]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_info_nickname_set():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $nickname=args_post("nickname");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="update `userInfo` set `nickname`='$nickname' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        echo json_encode(["status"=>true]);
        return true;
    }

    public static function user_info_gender_get():bool
    {
        self::autoLogin();
        $username = session_get("username");
        $conn = connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            echo json_encode(["status"=>true,"data"=>$row["gender"]]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_info_gender_set():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $gender=args_post("gender");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="update `userInfo` set `gender`='$gender' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        echo json_encode(["status"=>true]);
        return true;
    }

    public static function user_info_age_get():bool
    {
        self::autoLogin();
        $username = session_get("username");
        $conn = connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            echo json_encode(["status"=>true,"data"=>$row["age"]]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_info_age_set():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $age=args_post("age");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="update `userInfo` set `age`='$age' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        echo json_encode(["status"=>true]);
        return true;
    }

    public static function user_info_description_get():bool
    {
        self::autoLogin();
        $username = session_get("username");
        $conn = connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            echo json_encode(["status"=>true,"data"=>$row["description"]]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_info_description_set():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $description=args_post("description");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="update `userInfo` set `description`='$description' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        echo json_encode(["status"=>true]);
        return true;
    }

    public static function user_info_avatar_get():bool
    {
        self::autoLogin();
        $username = session_get("username");
        $conn = connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql = "select * from `userInfo` where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        if(mysqli_num_rows($result)>0){
            $row = mysqli_fetch_assoc($result);
            echo json_encode(["status"=>true,"data"=>$row["avatar"]]);
        }else{
            die(json_encode(["status"=>false,"msg"=>"用户不存在"]));
        }
        return true;
    }

    public static function user_info_avatar_set():bool
    {
        self::autoLogin();
        $username=session_get("username");
        $avatar=args_post("avatar");
        $conn=connectDB();
        if(!$conn){
            die(json_encode(["status"=>false,"msg"=>"数据库连接失败：".mysqli_connect_error()]));
        }
        $sql="update `userInfo` set `avatar`='$avatar' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die(json_encode(["status"=>false,"msg"=>"数据库执行失败：".mysqli_error($conn)]));
        }
        echo json_encode(["status"=>true]);
        return true;
    }

    public static function user_bind_iurt_callback():bool
    {
        $iurt=args_post("username");
        $username=cookie_get("xcgzs_username");
        session_set("username",$username);
        cookie_delete("xcgzs_username");
        $isLogin=cookie_get("xcgzs_isLogin");
        session_set("isLogin",$isLogin);
        cookie_delete("xcgzs_isLogin");
        $conn=connectDB();
        if(!$conn){
            loadErrorPage("错误","数据库连接失败：".mysqli_connect_error());
        }
        $sql="update `userBind` set `iurt`='$iurt' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            loadErrorPage("错误","数据库执行失败：".mysqli_error($conn));
        }
        loadErrorPage("提示","用户绑定成功，IURT用户名为：$iurt",false);
        echo "<script>setTimeout(()=>{location.href='".linkProcess("{LINK_user#bind}")."'},1000);</script>";
        return true;
    }

    public static function user_bind_github_callback():bool
    {
        $github=session_get("bind_github_user");
        $username=cookie_get("xcgzs_username");
        session_set("username",$username);
        cookie_delete("xcgzs_username");
        $isLogin=cookie_get("xcgzs_isLogin");
        session_set("isLogin",$isLogin);
        cookie_delete("xcgzs_isLogin");
        session_delete("bind_github");
        session_delete("bind_github_user");
        $conn=connectDB();
        if(!$conn){
            loadErrorPage("错误","数据库连接失败：".mysqli_connect_error());
        }
        $sql="update `userBind` set `github`='$github' where `username`='$username';";
        if(cookie_get("dev")=="yes"){
            echo $sql;
        }
        $result=mysqli_query($conn,$sql);
        if(!$result){
            loadErrorPage("错误","数据库执行失败：".mysqli_error($conn));
        }
        loadErrorPage("提示","用户绑定成功，Github用户名为：$github",false);
        echo "<script>setTimeout(()=>{location.href='".linkProcess("{LINK_user#bind}")."'},1000);</script>";
        return true;
    }

    public static function user_bind_qq_callback():bool
    {
        print_r($_POST);
        $qq=args_post("username");
        $username=cookie_get("xcgzs_username");
        session_set("username",$username);
        cookie_delete("xcgzs_username");
        $isLogin=cookie_get("xcgzs_isLogin");
        session_set("isLogin",$isLogin);
        cookie_delete("xcgzs_isLogin");
        $conn=connectDB();
        if(!$conn){
            loadErrorPage("错误","数据库连接失败：".mysqli_connect_error());
        }
        $sql="update `userBind` set `qq`='$qq' where `username`='$username';";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            loadErrorPage("错误","数据库执行失败：".mysqli_error($conn));
        }
        loadErrorPage("提示","用户绑定成功，QQ昵称为：$qq",false);
        echo "<script>setTimeout(()=>{location.href='".linkProcess("{LINK_user#bind}")."'},1000);</script>";
        return true;
    }
}